package org.jeecg.modules.demo.cms.article.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.cms.article.entity.CmsArticle;
import org.jeecg.modules.demo.cms.article.mapper.CmsArticleMapper;
import org.jeecg.modules.demo.cms.article.service.ICmsArticleService;
import org.jeecg.modules.demo.cms.article.vo.CmsArticleVo;
import org.jeecg.modules.demo.cms.article_content.entity.CmsArticleContent;
import org.jeecg.modules.demo.cms.article_content.mapper.CmsArticleContentMapper;
import org.jeecg.modules.demo.cms.article_content.service.ICmsArticleContentService;
import org.jeecg.modules.demo.cms.article_deljl.entity.CmsArticleDeljl;
import org.jeecg.modules.demo.cms.article_deljl.service.ICmsArticleDeljlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;



/**
 * @Description: 网站新闻表
 * @Author: jeecg-boot
 * @Date:   2025-06-07
 * @Version: V1.0
 */
@Service
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArticle> implements ICmsArticleService {

    @Autowired
    private CmsArticleContentMapper cmsArticleContentMapper;
    @Autowired
    private ICmsArticleContentService cmsArticleContentService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(CmsArticleVo cmsArticleVo) {
        CmsArticle cmsArticle = new CmsArticle();
        BeanUtils.copyProperties(cmsArticleVo,cmsArticle);
        CmsArticleContent cmsArticleContent = new CmsArticleContent();
        BeanUtils.copyProperties(cmsArticleVo,cmsArticleContent);
        this.updateById(cmsArticle);
        //判断内容表是否有内容
        CmsArticleContent cmsArticleContent1 = cmsArticleContentService.getById(cmsArticleContent.getId());
        if(ObjectUtil.isNotNull(cmsArticleContent1)&& StrUtil.isNotEmpty(cmsArticleContent1.getId())){
            cmsArticleContentService.updateById(cmsArticleContent);
        }else{
            cmsArticleContentService.save(cmsArticleContent);
        }
    }

    @Override
    public IPage<CmsArticleVo> pageSelectArticle(Page<CmsArticleVo> page, Wrapper<CmsArticle> queryWrapper) {
//        return null;
        return this.getBaseMapper().pageSelectArticle(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCmsArticleVo(CmsArticleVo cmsArticleVo) {
        CmsArticle cmsArticle = new CmsArticle();
        BeanUtils.copyProperties(cmsArticleVo,cmsArticle);
        CmsArticleContent cmsArticleContent = new CmsArticleContent();

        this.save(cmsArticleVo);
        BeanUtils.copyProperties(cmsArticleVo,cmsArticleContent);
        cmsArticleContentService.save(cmsArticleContent);

    }

    @Autowired
    private ICmsArticleDeljlService cmsArticleDeljlService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteArticleById(String id) {
        CmsArticle cmsArticle = this.getById(id);
        CmsArticleDeljl cmsArticleDeljl = new CmsArticleDeljl();
        cmsArticleDeljl.setId(id);
        cmsArticleDeljl.setCid(id);
        cmsArticleDeljl.setTitle(cmsArticle.getTitle());
        cmsArticleDeljl.setArticletype(cmsArticle.getCategoryid());
        cmsArticleDeljlService.save(cmsArticleDeljl);

        this.getBaseMapper().deleteById(id);

    }

}
